const sha1 = require("sha1")
let {weChatConfig} = require('../config/AuthConfig')
const request = require('request')
exports.getNewToken = async () => {
    return new Promise((resolve, reject) => {
        let token_url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${weChatConfig.appId}&secret=${weChatConfig.appSecret}`;
        request(token_url, function (error, response, body) {
            if (!error) {
                console.log('成功1：error, response, body', typeof body, body)
                resolve(JSON.parse(body))
            } else {
                console.log('error：', error)
                reject(error)
            }
        });

    })
}

exports.getNewTicket = (access_token) => {
    console.log('access_token', access_token);
    return new Promise((resolve, reject) => {
        let ticket_url = `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`;
        request(ticket_url, function (error, response, body) {
            if (!error) {
                console.log('成功2：error, response, body', body)
                resolve(JSON.parse(body))
            } else {
                console.log('error：', error)
                reject(error)
            }
        });
    })
}

exports.getQr_ticket = (data, access_token) => {
    const url = `https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=${access_token}`
    return new Promise((resolve, reject) => {
        request.post(
            {
                url,
                json: data
            },
            async function (error, response, body) {
                if (!error) {
                    resolve(body)
                } else {
                    reject(JSON.parse(error))
                }
            }
        );
    })
}


exports.getUserInfo = (obj) => {
    return new Promise((resolve, reject) => {
        let url = `https://api.weixin.qq.com/sns/userinfo?access_token=${obj.access_token}&openid=${obj.openid}&lang=zh_CN`
        request(url, function (error, response, body) {
            if (!error) {
                console.log('getUserInfo_成功:, response, body', typeof body, body)
                // resolve(JSON.parse(body))\
                resolve(JSON.parse(body))
            } else {
                console.log('error：', error)
                // reject(error)
                reject(error)
            }
        });
    })
}

exports.getRight = async (right_url) => {
    return new Promise((resolve, reject) => {
        request(right_url, function (error, response, body) {
            if (!error) {
                console.log('getRight_成功11：error, response, body', typeof body, body)
                resolve(true)
            } else {
                console.log('error：', error)
                reject(error)
            }
        });
    })
}

exports.getValidateString = (req) => {
    let {token} = weChatConfig;
    console.log('我的token:', token);
    let {signature, echostr, timestamp, nonce} = req.query;
    // 将 token, timestamp, nonce 三项按照字典排序
    let arr = [token, timestamp, nonce];
    arr = arr.sort();
    console.log('sort-arr', arr);
    let arrStr = arr.join('');
    const relStr = sha1(arrStr);
    console.log('relStr', relStr);
    return relStr;
}